package com.situ.root.user.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections4.Get;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonValueFormatVisitor;
import com.situ.root.user.model.UserModel;
import com.situ.root.user.service.IUSerService;

import net.sf.json.JSONArray;
//import net.sf.json.JSONObject;

@Controller
@RequestMapping("/roots")
public class UserController {

	@Autowired
	private IUSerService userService;
	
	@RequestMapping(value = "/add")
	@ResponseBody
	public String add(UserModel userModel) {
		int res = userService.insert(userModel);
		System.out.println("res=" + res);
		if (res == 1) {
			return "1";
		}else {
			return "0";
		}
	}
//	,method= {RequestMethod.GET,RequestMethod.POST}
	@RequestMapping(value = "/list", produces={"application/json;charset=UTF-8"},method= {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public List<UserModel> list(UserModel userModel) {
		List<UserModel> res = userService.selectList(userModel);
		res = JSONArray.fromObject(res);
		System.out.println("res=" + res);
		return res;
	}
	
	@RequestMapping(value = "/upd",method= {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String upd(UserModel userModel) {
		int res = userService.update(userModel);
		System.out.println("res="+res);
		return "1";
	}
	
	@RequestMapping(value = "/get", produces={"application/json;charset=UTF-8"},method= {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public List<UserModel> get(UserModel userModel) {
		List<UserModel> res = userService.selectList(userModel);
		res = JSONArray.fromObject(res);
		System.out.println("get="+res);
		return res;
	}
	
	
	@RequestMapping(value = "/del",method= {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String del(UserModel userModel,Object id) {
		int res = userService.delete(userModel);
		System.out.println("res=" + res);
		return "1";
	}
	
	@RequestMapping(value = "/page",method= {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Map<String, Object> page(UserModel model) {
//		model.setPageIndex(Integer.parseInt(request.getParameter("pageIndex")));
//        model.setPageLimit(Integer.parseInt(request.getParameter("pageLimit")));
		model.setPageOn(true);
		Map<String,Object> map = new HashMap<>();
		map.put("data", userService.selectList(model));
        map.put("count", userService.selectCount(model));
		map = net.sf.json.JSONObject.fromObject(map);
		System.out.println("map=" + map);
		return map;
	}
	
	@RequestMapping(value = "/login", produces={"application/json;charset=UTF-8"},method= {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String login(UserModel model) {
		int res = userService.login(model);
		System.out.println("get====="+res);
		if(res==1) {
		return "1";}else if(res==0) {return "0";}else return "2";
	}
}
